import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import MessageHandler from "@runtime/module/Handler/MessageHandler";
import ModalComponent, { ModalOptions } from "@runtime/module/Modal/ModalComponent";
import InteractableObjectComponent from "@runtime/UnityEngineBridge/InteractableObjectComponent";
import PostProcessLayerComponent from "@runtime/UnityEngineBridge/PostProcessLayerComponent";
import { InstallComponent } from '@components/Apps/Lidar/LidarComponent';

/**
 * 交互对象位置应用消息
 */
export default class ApplyInteractableObjectLocationMessageHandler extends MessageHandler<ClientMessage.ApplyInteractableObjectLocationMessage> {
    protected async RunAsync(message: ClientMessage.ApplyInteractableObjectLocationMessage): Promise<void> {
        if(sessionStorage.getItem('VolumeId')) {
            await PostProcessLayerComponent.Default.RemoveVolumeAsync(sessionStorage.getItem('VolumeId'));
        }
        
        InteractableObjectComponent.Default.OnApplyLocationAsync(message);
        InstallComponent.Default.StepDescriptions.value.forEach((element:any) => {
            if(message.Name == element.name.substring(element.name.indexOf('装')+1)) {
                element.type = message.Success
            }
        });
        if (!message.Success) {
            const volume = await PostProcessLayerComponent.Default.AddVolumeAsync();
            const effect = await PostProcessLayerComponent.Default.AddKawaseBlurAsync(volume);

            const options = ModalOptions.Default()
                .WithPrefix('/src/components/Apps/Lidar/')
                .WithType('MessageBox')
                .WithContent(message.Description)
                .WithButtons(['确定']);
            await ModalComponent.Default.PopupAsync(options);

            await PostProcessLayerComponent.Default.RemoveEffectAsync(volume, effect);
            await PostProcessLayerComponent.Default.RemoveVolumeAsync(volume);
        }
    }
}
